package com.work.keep.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.work.keep.R;
import com.work.keep.adapter.MyFollowsRecyclerAdapter;
import com.work.keep.adapter.OnColorChangable;
import com.work.keep.base.BaseFragment;
import com.work.keep.datas.DynamicDiscoverData;
import com.work.keep.ui.PullToRefreshListView;
import com.work.keep.volley.GsonRequest;
import com.work.keep.volley.VolleySingleton;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by dllo on 16/1/12.
 */
public class FollowsFragment extends BaseFragment {
    private Context context;
    private RecyclerView recyclerView;
    private VolleySingleton volleySingleton;
    private MyFollowsRecyclerAdapter myFollowsRecyclerAdapter;
    private OnColorChangable onColorChangable;

    public void setOnColorChangable(OnColorChangable onColorChangable) {
        this.onColorChangable = onColorChangable;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = (RecyclerView) view.findViewById(R.id.followsRecycler);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_followings, container, false);
        return v;
    }

    @Override
    protected void init() {
        volleySingleton = VolleySingleton.getInstance();
        myFollowsRecyclerAdapter = new MyFollowsRecyclerAdapter(context);
        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(myFollowsRecyclerAdapter);
        initRecyclerData();
    }
    private void initRecyclerData() {
        String url = "https://api.gotokeep.com/v1.1/timeline/hot";
        GsonRequest<DynamicDiscoverData> request = new GsonRequest<DynamicDiscoverData>(url, DynamicDiscoverData.class, new Response.Listener<DynamicDiscoverData>() {
            @Override
            public void onResponse(DynamicDiscoverData response) {
                myFollowsRecyclerAdapter.addFollowsDataList(response.getData());
                recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                        super.onScrolled(recyclerView, dx, dy);
//                        Log.e("aa", "" + dy + "");
                        View view = recyclerView.getChildAt(0);
                        RecyclerView.ViewHolder holder = recyclerView.getChildViewHolder(view);
                        int position = recyclerView.getChildAdapterPosition(view);
//                        if (holder.getLayoutPosition() > 3) {
                            int color = ((MyFollowsRecyclerAdapter.MyFollowsViewHolder) holder).getColor();
                            if (onColorChangable != null) {
                                onColorChangable.changeColor(color);
//                            }
                        }

                    }
                });

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("aaa", "eee");
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> headers = new HashMap<>();
                headers.put("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfaWQiOiI1NjY1MDBlMTYzZGIwYzlmNTdiMTQwYWQiLCJ1c2VybmFtZSI6Iuaeq-WQmyIsImF2YXRhciI6Imh0dHA6Ly93eC5xbG9nby5jbi9tbW9wZW4vc2t3MmhSR3lXRWcwUzV0NzhkMlRVUnFhYjBzcWJ4RXB4S1BLUFpEOXpwbTNJbkIySzRjRnhLVEJsemdDb3VIcGlia3RkRUluQ0NFcGpIS2FHZ1lwRkp6SVRIVU5NYTNpYmYvMCIsImlhdCI6MTQ1MjU4OTkwNiwiZXhwIjoxNDU1MTgxOTA2LCJpc3MiOiJodHRwOi8vd3d3LmdvdG9rZWVwLmNvbS8ifQ.4I6FpbJT1_1fe8Z51oHt6Mv5t-3zXGGYhI1rMe3U8gM");
                headers.put("X-X", "486bba2566a02d3ae31ed4445366a873b89cf910");
                headers.put("Host", "api.gotokeep.com");
                headers.put("X-KEEP-VERSION", "2.8.1");
                headers.put("X-KEEP-CHANNEL", "keep");
                return headers;
            }


        };
        volleySingleton._addRequest(request);
    }
}
